<?php

class UserAPIController extends \BaseController {

	/**
	 * Controller constructor
	 */
	public function __construct() {
	}
	
	/**
	 * Display a listing of users.
	 *
	 * @return Response
	 */
	public function index()
	{
		return Response::json(User::all());
	}


	/**
	 * Store a newly created user in storage from JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function store()
	{
		$newUser = Input::json();
	
		$user = new User();
		$user->fbId = $newUser->get('fbId');
		$user->name = $newUser->get('name');
		$user->lastname = $newUser->get('lastname');
		$user->photo_url = $newUser->get('photo_url');
		$user->age = $newUser->get('age');
		$user->sex = $newUser->get('sex');
		$user->fanOf = $newUser->get('fanOf');
		$user->location = $newUser->get('location');
		$user->email = $newUser->get('email');
		$user->FBToken = $newUser->get('FBToken');
		$user->save();
	
		return Response::json($user);
	}


	/**
	 * Display the specified user.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		try{
			$user = User::where('fbId', '=', $id)->firstOrFail();
			if(is_null($user)){
				return Response::json('User not found', 404);
			} else {
				return Response::json($user);
			}
		} catch(Exception $e) {
			return Response::json('User not found', 404);
		}
	}


	/**
	 * Update the specified user in storage from JSON put data.
	 *
	 * @return JSON Response
	 */
	public function update($id)
	{
		$updateUser = Input::json();
	
		$user = User::find($id);
		if(is_null($user)){
			return Response::json('User not found', 404);
		}
		$user->fbId = $updateUser->get('fbId');
		$user->name = $updateUser->get('name');
		$user->lastname = $updateUser->get('lastname');
		$user->photo_url = $updateUser->get('photo_url');
		$user->age = $updateUser->get('age');
		$user->sex = $updateUser->get('sex');
		$user->fanOf = $updateUser->get('fanOf');
		$user->location = $updateUser->get('location');
		$user->email = $updateUser->get('email');
		$user->FBToken = $updateUser->get('FBToken');
		$user->save();
	
		return Response::json($user);
	}


	/**
	 * Remove the specified user from storage by posted id.
	 *
	 * @return JSON Response
	 */
	public function destroy($id)
	{
		$user = User::find($id);
	
		if(is_null($user))
		{
			return Response::json('User not found', 404);
		}
		$deletedUser = $user;
		$user->delete();
		return Response::json($deletedUser);
	}
	
	/**
	 * Display a listing of matches by user id in JSON format.
	 *
	 * @param $userId
	 * @return JSON Response
	 */
	public function getMatches($userId)
	{
		$user = User::find($userId);
		if(is_null($user)){
			return Response::json('User not found', 404);
		} else {
			$confirmed = array();
				
			/*foreach($user->teams as $key => $value){
				foreach($value->matches as $match){
					//players confirmed
					$guestsConfirmedAmount = $match->guests()->wherePivot('confirmed', 1)->count() + 1;
					$match->guestsConfirmedAmount = $guestsConfirmedAmount;
			
					$match->admin;
					$match->field;
					$match->type;
					
					if($match->admin->id != $userId)
						$confirmed[] = $match;
				}
			}
			foreach($user->matchesSub as $match){
				//players confirmed
				$guestsConfirmedAmount = $match->guests()->wherePivot('confirmed', 1)->count() + 1;
				$match->guestsConfirmedAmount = $guestsConfirmedAmount;
				
				$match->admin;
				$match->field;
				$match->type;
			}*/
			$matchesGuest = array();
			foreach($user->matchesGuest()->where('cancelled', '=', 0)->get() as $match){
				//players confirmed
				$guestsConfirmedAmount = $match->guests()->wherePivot('confirmed', 1)->count() + 1;
				$match->guestsConfirmedAmount = $guestsConfirmedAmount;
				
				$match->admin;
				$match->field;
				$match->type;
				$match->substitutes;
				$match->guests;
				foreach($match->teams as $value){
					$value->users;
				}
				
				$matchesGuest[] = $match;
			}
			$matchesAdmin = array();
			foreach($user->matchesAdmin()->where('cancelled', '=', 0)->get() as $match){
				//players confirmed
				$guestsConfirmedAmount = $match->guests()->wherePivot('confirmed', 1)->count() + 1;
				$match->guestsConfirmedAmount = $guestsConfirmedAmount;
				
				$match->admin;
				$match->field;
				$match->type;
				$match->substitutes;
				$match->guests;
				foreach($match->teams as $value){
					$value->users;
				}
				
				$matchesAdmin[] = $match;
			}
			
			return Response::json(array(
					//"Sub" => $user->matchesSub()->where('cancelled', 0),
					"Guest" => $matchesGuest,
					"Admin" => $matchesAdmin//,
					//"Comfirmed" => $confirmed
				));
		}
	}
	
	/**
	 * Display a listing of groups by user id in JSON format.
	 *
	 * @param $userId
	 * @return JSON Response
	 */
	public function getGroups($userId)
	{
		$user = User::find($userId);
		if(is_null($user)){
			return Response::json('User not found', 404);
		} else {
			foreach($user->groups as $group){
				$group->users;
			}
			foreach($user->groupsAdmin as $group){
				$group->admin;
			}
			return Response::json(array(
					"Member" => $user->groups,
					"Admin" => $user->groupsAdmin
				));
		}
	}

}
